package com.mijie.query;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.apache.log4j.Logger;

import com.mijie.homi.common.domain.mood.Mood;
import com.mijie.homi.common.domain.topic.Topic;
import com.mijie.homi.search.util.DbNdsController;
import com.mijie.homi.search.util.Dbcontroller;
import com.mijie.homi.search.util.fileC0;
import com.mijie.homi.search.util.god;
import com.mijie.homi.search.util.listUtil;
import com.mijie.homi.search.util.strUtil;
import com.mijie.indexall.indexControllor4auto;

public class sincinController {

	public static final Object sincin = "sc";
	private static String jenmye = "正面";
	private static Map map4cinsyv;

	/**
	 * 开心1,狂喜5,伤心3,郁闷4,纠结7,愤怒2,委屈8,无聊6,无感9
	 */
	public static String getMood_cn(int mood) {
		String s = "开心1,狂喜5,伤心3,郁闷4,纠结7,愤怒2,委屈8,无聊6,无感9";
		String[] a = s.split(",");
		Map<Integer, String> mp = new HashMap<Integer, String>();
		for (String moodx : a) {
			String mood_cn = strUtil.left(moodx, 2);
			Integer mood_id = Integer.parseInt(strUtil.right(moodx, 1));
			mp.put(mood_id, mood_cn);
			// System.out.println();
		}

		return mp.get(mood);
	}

	public static boolean isJenmyerSincin(int mood) {
		String sincin = getMood_cn(mood);
		LuceneIndex l = new LuceneIndex(5);
		String cinsyv = l.getcinsyv(sincin);

		if (cinsyv.equals(sincinController.jenmye))
			return true;
		else
			return false;
	}

	public static String getcinsyv(String sincin) {
		if (map4cinsyv == null) {
			String s = "开心 正面|狂喜 正面|||无聊 中性|无感 中性|||气愤 负面|伤心 负面|郁闷 负面||纠结 负面|委屈 负面|愤怒 负面||";
			Map mpca29 = listUtil.fromStr(s, " ", "\\|");
			@SuppressWarnings("unused")
			fileC0 fc = new fileC0();
			// List<String> li = fc.fileRead2list("C:\\renvaEvent\\cinsyv.txt");
			Map mp = new HashMap<String, String>();
			mp = mpca29;
			// mp.put(sincin,"正面");
			// for (String line : li) {
			// String[] a = line.split(" ");
			// if (a.length < 2) {
			// System.out.println("--warnin:" + line);
			// continue;
			// }
			// String sincinx = a[0].trim();
			// String cinsyv = a[1].trim();
			// mp.put(sincinx, cinsyv);
			// }
			map4cinsyv = mp;
			map4cinsyv.put("中性", "中性");
			map4cinsyv.put("正面", "正面");
			map4cinsyv.put("负面", "负面");
		}
		// return mp.get(sincin);

		String string = (String) map4cinsyv.get(sincin);
		if (string == null) {
			fulltxtControllor.logger.warn(" cant get cinsyv: from sincin:"
					+ sincin);
			return "中性";
		}
		return string;
	}

	private static Logger logger = Logger.getLogger(new indexControllor4auto()
			.getClass());
	public static String sincinlib = "sincinlib";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println(isJenmyerSincin(7));
	}

	public Mood getLast() {
		String sql = "";
		String id = "";
		try {

			sql = "select * from mood  where length(content)>3 and id not in( select mid  from  mood4full where fromx='sincinlib' ) order by createAt desc limit 1";
			id = Dbcontroller.getIdsX(sql);
			if (id.length() == 0) {
				logger.info(" get last mood empty ");
				return null;
			}

			Mood m = new Mood();
			m.setId(Long.parseLong(id));
			return m;
		} catch (Exception e) {
			logger.error(sql);
			logger.error("get ids" + id);
			logger.error(god.getTrace(e));
			return null;
		}

	}

	public Topic getLast_prblm() {
		String sql = "";
		String id = "";
		try {

			sql = " select * from topic where   length(title)>3 and id not in( select mid  from  mood4full where fromx='problemlib' ) order by createAt desc limit 1";

			id = Dbcontroller.getIdsX(sql);
			if (id.length() == 0) {
				logger.info(" get last prblm empty ");
				return null;
			}

			Topic m = new Topic();
			m.setId(Integer.parseInt(id));
			return m;
		} catch (Exception e) {
			logger.error(sql);
			logger.error("get ids" + id);
			logger.error(god.getTrace(e));
			return null;
		}
	}

	public static String getMood_cn(String moodID) {
		return getMood_cn(Integer.parseInt(moodID));

	}

	public static boolean cinsyvMatch(String moodCn, Object moodID) {
		String cinsyv = getcinsyv(moodCn);

		String cinsyv2 = getcinsyvByMooid(moodID);
		if (cinsyv.equals(cinsyv2))
			return true;
		else
			return false;
	}

	private static String getcinsyvByMooid(Object moodID) {
		int n = Integer.parseInt(moodID.toString());
		String mood = getMood_cn(n);
		return getcinsyv(mood);
	}

}
